package com.smartwebx.ha.web.admin.module.screen;

import static com.smartwebx.ha.web.admin.common.HaConstant.HA_USER_SESSION_KEY;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.requestcontext.parser.ParameterParser;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.biz.rainwater.real.StationManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.dal.realtime.dao.RainfallMapper;
import com.smartwebx.ha.web.admin.common.HaUserSys;

public class OtherLogin {
	@Autowired
	private HaUserManager haUserManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private StationManager stationManager;

	@Autowired
	private RainfallMapper rainfallDao;

	public void execute(@Param("name") String name, @Param("pass") String pass,
			HttpSession session, Navigator nav, ParameterParser params)
			throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> result = Maps.newHashMap();

		Entry<Integer, HaUser> haUsre = haUserManager.loginUser(name, pass);

		if (haUsre.getKey() == 1) {
			// 在session中创建petstoreUser对象
			HaUserSys haUserSys = (HaUserSys) session
					.getAttribute(HA_USER_SESSION_KEY);
			if (haUserSys == null || haUserSys.hasLoggedIn()) {
				haUserSys = new HaUserSys();
			}
			HaUser user = haUsre.getValue();
			HaArea area = haAreaManager.findOne(user.getAreaId());

			haUserSys.upgrade(user.getName(), new String[] { "admin" },
					user.getId(), user.getAreaId(), user.getRoleId(),
					area.getCode(), area.getGroupId());

			session.setAttribute(HA_USER_SESSION_KEY, haUserSys);

			Cookie groupIdCookie = new Cookie("groupId", area.getGroupId()
					.toString());
			groupIdCookie.setMaxAge(3600 * 24);
			response.addCookie(groupIdCookie);

			Cookie areaIdCookie = new Cookie("areaId", area.getId().toString());
			areaIdCookie.setMaxAge(3600 * 24);
			response.addCookie(areaIdCookie);

			Cookie parentIdCookie = new Cookie("parentId", area.getParentId()
					.toString());
			parentIdCookie.setMaxAge(3600 * 24);
			response.addCookie(parentIdCookie);

			Cookie userIdCookie = new Cookie("userId", user.getId().toString());
			userIdCookie.setMaxAge(3600 * 24);
			response.addCookie(userIdCookie);

			Cookie codeCookie = new Cookie("code", user.getCode());
			codeCookie.setMaxAge(3600 * 24);
			response.addCookie(codeCookie);

			Cookie roleIdCookie = new Cookie("roleId", user.getRoleId()
					.toString());
			roleIdCookie.setMaxAge(3600 * 24);
			response.addCookie(roleIdCookie);

			result.put("isLogin", true);
			result.put("mess", "登录成功");
		} else {
			result.put("isLogin", false);
			result.put("mess", "登录失败，用户名或密码错误");
		}

		mapper.writeValue(out, result);
	}
}
